{extend name="public/baseLayuimini"}
{block name="title"}{/block}
{block name="css"}

{/block}
{block name="main"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote">
            公司管理，可以新增，修改，删除公司。<br>
        </blockquote>
        <div>
            <table id="test" class="layui-table" lay-filter="test"></table>
        </div>
    </div>
</div>
{/block}
{block name="js"}
<!--头部工具栏-->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="del-check">删除选中</button>
        <button class="layui-btn layui-btn-sm" lay-event="add-one">新增</button>
    </div>
</script>
<!--单元格工具栏-->
<script type="text/html" id="handle">
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
</script>
<script src="__LAYUIMINI__/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table','form','jquery','layer','miniTab'],function(){
        var table = layui.table,
            form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            miniTab = layui.miniTab
        table.render({
            elem: '#test'
            ,toolbar: '#toolbarDemo'
            ,url:'/admin/siteList'
            ,cellMinWidth: 80
            ,cols: [[
                {type: 'checkbox'}
                ,{field:'site_name', title:'站点名称', width:100, unresize: true}
                ,{field:'site_url', title:'网址', width:100, unresize: true}
                ,{field:'company_name', title:'公司', width:110, unresize: true}
                ,{field:'language', title:'语言', width:110, unresize: true}
                ,{field:'phone', title:'联系电话', templet: '#usernameTpl'}
                ,{field:'email', title:'邮箱'}
                ,{field:'keyword', title: '关键字', minWidth:120, sort: true}
                ,{field:'browsing', title:'浏览量', width:85, unresize: true}
                ,{templet: '#handle', width: 180, align: 'center', title: '操作'}
            ]]
            ,page: false
        });
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'del-check':
                    var data = checkStatus.data;
                    let ids = data.map(item=>item.id)
                    delSite(ids)
                    break;
                case 'add-one':
                    miniTab.openNewTabByIframe({
                        href:"/admin/editSite",
                        title:"公司编辑",
                    });
                    break;
            };
        });
        table.on('tool(test)',function(obj){
            if(obj.event == 'delete'){
                let ids = [obj.data.id]
                delSite(ids)
            }else{
                miniTab.openNewTabByIframe({
                    href:"/admin/editSite?id="+obj.data.id,
                    title:"公司编辑",
                });
            }
            console.log(obj)
        })
        function delSite(ids){
            layer.confirm('真的要删除选中的公司吗?',function(index){
                $.post('/admin/delSite',{ids},res=>{
                    layer.close(index)
                    table.reload('test')
                    if(res){
                        layer.msg('删除成功')
                    }else{
                        layer.msg('删除失败')
                    }
                })
            })
        }
    })
</script>
{/block}